package fun.werfamily.wf.goods.core.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.werfamily.wf.goods.core.domain.dto.request.BannerListReqDTO;
import fun.werfamily.wf.goods.core.domain.dto.response.BannerDTO;
import fun.werfamily.wf.goods.core.domain.service.IBannerService;
import fun.werfamily.wf.goods.core.infrastructure.dataobject.BannerDO;
import fun.werfamily.wf.goods.core.infrastructure.mapper.BannerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Mr.WenMing Zhang
 * @Date: 2022/06/27 14:51
 * @Description:
 **/
@Service
@Slf4j
public class IBannerServiceImpl extends ServiceImpl<BannerMapper, BannerDO> implements IBannerService {

    /**
     * 多条件查询
     *
     * @param bannerListReqDTO
     * @return
     */
    @Override
    public List<BannerDTO> listByParams(BannerListReqDTO bannerListReqDTO) {
        List<BannerDTO> bannerDTOS = new ArrayList<>();
        QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BannerDO::getIsDeleted, 0);
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(bannerListReqDTO.getPosition()), BannerDO::getPosition, bannerListReqDTO.getPosition());
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(bannerListReqDTO.getStatus()), BannerDO::getStatus, bannerListReqDTO.getStatus());
        queryWrapper.lambda().eq(null != bannerListReqDTO.getSort(), BannerDO::getSort, bannerListReqDTO.getSort());
        queryWrapper.lambda().in(CollectionUtils.isNotEmpty(bannerListReqDTO.getClientTypeList()), BannerDO::getClientType, bannerListReqDTO.getClientTypeList());
        queryWrapper.orderByDesc("sort");
        queryWrapper.orderByDesc("id");
        List<BannerDO> bannerDOS = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(bannerDOS)) {
            for (BannerDO bannerDO : bannerDOS) {
                BannerDTO bannerDTO = new BannerDTO();
                BeanUtils.copyProperties(bannerDO, bannerDTO);
                bannerDTOS.add(bannerDTO);
            }
        }
        return bannerDTOS;
    }

    /**
     * 后台分页查询
     *
     * @param bannerListReqDTO
     * @return
     */
    @Override
    public Page<BannerDTO> page(BannerListReqDTO bannerListReqDTO) {
        QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BannerDO::getIsDeleted, 0);
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(bannerListReqDTO.getPosition()), BannerDO::getPosition, bannerListReqDTO.getPosition());
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(bannerListReqDTO.getStatus()), BannerDO::getStatus, bannerListReqDTO.getStatus());
        queryWrapper.lambda().like(StringUtils.isNotEmpty(bannerListReqDTO.getBannerName()), BannerDO::getBannerName, bannerListReqDTO.getBannerName());
        queryWrapper.lambda().in(CollectionUtils.isNotEmpty(bannerListReqDTO.getClientTypeList()), BannerDO::getClientType, bannerListReqDTO.getClientTypeList());
        queryWrapper.lambda().gt(null != bannerListReqDTO.getStartTime(), BannerDO::getCreateTime, bannerListReqDTO.getStartTime());
        queryWrapper.lambda().lt(null != bannerListReqDTO.getEndTime(), BannerDO::getCreateTime, bannerListReqDTO.getEndTime());
        queryWrapper.orderByDesc("id");
        Page<BannerDO> page = this.page(new Page<BannerDO>(bannerListReqDTO.getPageNum(), bannerListReqDTO.getPageSize()), queryWrapper);
        if (page == null || CollectionUtils.isEmpty(page.getRecords())) {
            return new Page<>();
        }
        List<BannerDTO> bannerDTOS = new ArrayList<>();
        List<BannerDO> records = page.getRecords();
        for (BannerDO record : records) {
            BannerDTO bannerDTO = new BannerDTO();
            BeanUtils.copyProperties(record, bannerDTO);
            bannerDTOS.add(bannerDTO);
        }
        Page<BannerDTO> bannerDTOPage = new Page<>();
        bannerDTOPage.setRecords(bannerDTOS);
        bannerDTOPage.setTotal(page.getTotal());
        bannerDTOPage.setSize(page.getSize());
        bannerDTOPage.setPages(page.getPages());
        return bannerDTOPage;
    }

    /**
     * 根据排序查询
     *
     * @param sort
     * @param position
     * @param clientType
     * @return
     */
    @Override
    public BannerDO getBySort(Integer sort, String position, Byte clientType) {
        QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BannerDO::getIsDeleted, 0);
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(position), BannerDO::getPosition, position);
        queryWrapper.lambda().eq(BannerDO::getClientType, clientType);
        queryWrapper.lambda().eq(null != sort, BannerDO::getSort, sort);
        return getOne(queryWrapper);
    }
}
